package com.example.Servlet;

import com.example.Service.UserService;
import com.example.pojo.News;
import com.example.pojo.User;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "InfoUserServlet", value = "/InfoUserServlet")
public class InfoUserServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        response.setContentType("text/html");
        String id=request.getParameter("id");
        //2 调用业务逻辑，完成查询功能
        UserService userService=new UserService();
        User n=userService.getUserInfo(Integer.parseInt(id));

        String relativePath = n.getUserAuator();
        String baseUrl = "http://localhost:8080/mydemo_war/images/";
        String fullImgUrl = baseUrl + relativePath;
        if (relativePath == null || relativePath.trim().isEmpty()) {
            n.setUserAuator("");
        } else {
            n.setUserAuator(fullImgUrl);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("code",200);
        map.put("msg","查询成功");
        map.put("data",n);
        Gson gson=new Gson();
        String json= gson.toJson(map);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().append(json);
    }

    public void destroy() {
    }
}